﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SharePoint;
using RaisingStudio.Data.Providers;
using RaisingStudio.Data;
using RaisingStudio.Data.SharePoint.Entities;
using RaisingStudio.Data.Linq;

#if NET_2_0
using RaisingStudio.Linq;
using Microsoft.SharePoint;
#else
using System.Linq;
#endif

namespace SharePointWindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            SharePointConnection connection = new SharePointConnection("http://moss/blank");
            SharePointCommand command = connection.CreateCommand() as SharePointCommand;
            command.ListUrl = "/Lists/SampleCustomList";
            SharePointDataAdapter dataAdapter = new SharePointDataAdapter();
            dataAdapter.SelectCommand = command;
            System.Data.DataTable dataTable = new System.Data.DataTable();
            dataAdapter.Fill(dataTable);
            this.dataGridView1.DataSource = dataTable;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SharePointConnection connection = new SharePointConnection("http://moss/Docs");
            SharePointCommand command = connection.CreateCommand() as SharePointCommand;
            command.ListUrl = "/Lists/Contract List";
            SharePointDataAdapter dataAdapter = new SharePointDataAdapter();
            dataAdapter.SelectCommand = command;
            System.Data.DataTable dataTable = new System.Data.DataTable();
            dataAdapter.Fill(dataTable);
            this.dataGridView1.DataSource = dataTable;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SharePointConnection connection = new SharePointConnection("http://moss/Docs");
            SharePointCommand command = connection.CreateCommand() as SharePointCommand;
            command.ListUrl = "/Lists/Contract List";
            command.CommandText = "<Where><Geq><FieldRef Name='Title'/><Value Type='Text'>b</Value></Geq></Where>";
            SharePointDataAdapter dataAdapter = new SharePointDataAdapter();
            dataAdapter.SelectCommand = command;
            System.Data.DataTable dataTable = new System.Data.DataTable();
            dataAdapter.Fill(dataTable);
            this.dataGridView1.DataSource = dataTable;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SharePointConnection connection = new SharePointConnection("http://moss/Docs");
            SharePointCommand command = connection.CreateCommand() as SharePointCommand;
            command.ListUrl = "/Lists/Contract List";
            command.CommandText = "<Where><Geq><FieldRef Name='Title'/><Value Type='Text'>b</Value></Geq></Where><OrderBy><FieldRef Name='Modified' Ascending='FALSE'/></OrderBy>";
            SharePointDataAdapter dataAdapter = new SharePointDataAdapter();
            dataAdapter.SelectCommand = command;
            System.Data.DataTable dataTable = new System.Data.DataTable();
            dataAdapter.Fill(dataTable);
            this.dataGridView1.DataSource = dataTable;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<ContractList>();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<ContractList>(ContractList._.Title >= "b");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<ContractList>(ContractList._.Title >= "b" ^ !ContractList._.Modified);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<ContractList>(ContractList._.Title >= "b" & ContractList._.Title < "c" ^ !ContractList._.Modified);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<ContractList>((ContractList._.Title >= "b" | ContractList._.Title < "c") ^ ContractList._.Modified);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            List<Documents> documents = dataContext.QueryForList<Documents>(Documents._.Title == "A");
            Console.WriteLine(documents.Count);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            QueryableData<Documents> documents = new QueryableData<Documents>(dataContext);
            var docs = from d in documents where d.Title == "A" select d;
            foreach (var d in docs)
            {
                Console.WriteLine(d.Title);
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            var documents = dataContext.Query<Documents>(Documents._.Title == "A");
            foreach (var doc in documents)
            {
                Console.WriteLine(doc.Title);
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            var doc = dataContext.GetData<Documents>(Documents._.Title == "A");
            Console.WriteLine(doc.Title);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<Documents>((Documents._.Title >= "A" & Documents._.Title != "B" & Documents._.Title != "C" | Documents._.Title < "C") ^ Documents._.Title);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<Documents>((((Documents._.Title >= "A" & Documents._.Title != "B") | (Documents._.Title != "C" & Documents._.Title < "D")) & Documents._.Title <= "E") ^ Documents._.Title);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<Documents>(Documents._.Modified == System.DateTime.Now);            
        }

        private void button11_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            CommonCommand command = new CommonCommand(string.Format("<WHERE><EQ><FieldRef Name='{0}'/><Today /></EQ></WHERE>", Documents._.Modified));
            List<Documents> docs = dataContext.ExecuteForList<Documents>(command);
            this.dataGridView1.DataSource = dataContext.ExecuteForDataTable<Documents>(command);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            dataContext.Log = Console.Out;
            List<Documents> docs = dataContext.QueryForList<Documents>(Documents._.Title == "<% Test & Test />this is a test file</Test>");
            this.dataGridView1.DataSource = dataContext.QueryForDataTable<Documents>(Documents._.Title == "<% Test & Test />this is a test file</Test>");

            CommonCommand command = new CommonCommand(string.Format("<WHERE><EQ><FieldRef Name='{0}'/><Today /></EQ></WHERE>", Documents._.Modified));
            SharePointDataReader reader2= dataContext.ExecuteReader<Documents>(command) as SharePointDataReader;
            if (reader2.Read())
            {
                SPListItemCollection listCollection2 = reader2.SharePointListItemCollection;
                Console.WriteLine(listCollection2.Count);
            }

            SharePointDataReader reader3 = dataContext.QueryReader<Documents>(Documents._.Title == "<% Test & Test />this is a test file</Test>") as SharePointDataReader;
            if (reader3.Read())
            {
                Console.WriteLine(reader3.SharePointListItemCollection.Count);
                foreach (SPListItem item in reader3.SharePointListItemCollection)
                {
                    if (item.File != null)
                    {
                        MessageBox.Show(item.File.Title);
                    }
                }
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            DataContext dataContext = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            IList<ContractList> contractList = dataContext.QueryForList<ContractList>(ContractList._.Title.IsNull());
            contractList = dataContext.QueryForList<ContractList>(ContractList._.Title.IsNotNull() & ContractList._.Title.Like("a", RaisingStudio.Data.Expressions.MatchOperator.Left));
            contractList = dataContext.QueryForList<ContractList>(ContractList._.Title.IsNotNull() & ContractList._.Title.Like("b", RaisingStudio.Data.Expressions.MatchOperator.Any));
            Console.WriteLine(contractList.Count);
        }

        private void button20_Click(object sender, EventArgs e)
        {
            DataContext dc = new DataContext(DatabaseType.SHAREPOINT, "http://moss/Docs");
            List<SampleCustomList> sampleCustomList = dc.QueryForList<SampleCustomList>();
            List<SampleCustomList> sampleCustomList2 = dc.QueryForList<SampleCustomList>(SampleCustomList._.Title == "a");
            List<SampleCustomList> scl3 = dc.QueryForList<SampleCustomList>(SampleCustomList._.ID, SampleCustomList._.Title);
            List<SampleCustomList> scl4 = dc.QueryForList<SampleCustomList>(SampleCustomList._.Title.Like("a"), SampleCustomList._.ID, SampleCustomList._.Title);
            Console.WriteLine(sampleCustomList.Count);

            var sc5 = dc.Query<SampleCustomList>();
            foreach (var sc in sc5)
            {
                Console.WriteLine(sc.Title);
            }

            var sc6 = dc.Query<SampleCustomList>(SampleCustomList._.Title.Like("a"));
            foreach (var sc in sc6)
            {
                Console.WriteLine(sc.Title);
            }
        }
    }
}
